//点击保存供货方式
function toSaveSupplyMethod(){
	var orderId=$("#orderDetailDiv>input").val();
	var goodsIdArray=new Array();
	var noGoodsIdArray=new Array();
	$("#orderDetailDiv").find("input[name='select']").each(function(i,inputs){
		if($(this).attr("checked")){
			goodsIdArray.push($(this).parent().next().html());
		}else{
			noGoodsIdArray.push($(this).parent().next().html());
		}

	});	

	if(goodsIdArray.length==0){
		goodsIdArray.push("1");
	}
	if(noGoodsIdArray.length==0){
		noGoodsIdArray.push("1");
	}
	$.post(
			"../orders/updateSupplyMethod.do",
			{
				orderId:orderId,
				goodsIdArray:goodsIdArray,
				noGoodsIdArray:noGoodsIdArray
			},
			function(data){
				if(data==goodsIdArray.length){
					success("配货方式选择成功!");
					setTimeout(myfresh,2000);
				}
			},
			"json"
	);

		

	//把订单号,以及该订单勾选的商品号,发送到后台,更改起配货方式
//	$.post(
//			"../orders/updateSupplyMethod.do",
//			{
//				orderId:orderId,
//				goodsIdArray:goodsIdArray
//			},
//			function(data){
//				if(data==goodsIdArray.length){
//					success("配货方式选择成功!");
//					setTimeOut(myfresh,2000);
//				}
//			},
//			"json"
//	);

	
}
//根据订单号获取他的所有商品(增加了选中框)
function choosePeiHuoOfGoods(orderId){
	$("#orderDetailDiv>table>tbody").empty();
	$("#orderDetailDiv>h3>span").empty();
	$("#orderDetailDiv>input").val(orderId);
	$.post("../orders/getOrderByOrderId.do",
			{orderId:orderId},
			function(data){
				$("#orderDetailDiv>h3>span").html(data.couponsId);
			},
			"json"
	);
	$.post("../orders/getGoodOfAccByOrderId.do",
		{
		orderId:orderId
		},
		function(data){
			$.each(data,function(i,goods){
				$("#orderDetailDiv>table>tbody").append(makeGoodsTr2(goods));
			});
			
		},
		"json"
	);
}
//点击全选
function allSelect(){
	//把全选的字, 变成反选
	if($("#allSelectButton").val()=="全选"){
		$("#allSelectButton").val("反选");
	}else{
		$("#allSelectButton").val("全选");
	}
	//全选
	$("#orderDetailDiv>table>tbody").find("input[type='radio']").each(function(i,radio){
		if($(this).prop("checked")){
			$(this).prop("checked",false);
		}else{
			$(this).prop("checked",true);
		}
	});
}


//点击顾客订单
function clickOrders(currentPage,status,afterSale){
	window.location.href="../orders/toNewOrders.do?currentPage="+currentPage+"&status="+status+"&afterSale="+afterSale
}

//根据订单号的得到他的所有商品
function findGoodsByOrderId(orderId){
	$("#orderDetailDiv>table>tbody").empty();
	$("#orderDetailDiv>table>tbody").append('<input type="hidden" value="'+orderId+'">');
	//这一步是修改model下的foot
	$.post(
			"../orders/getGoodOfAccByOrderId.do",
			{orderId:orderId},
			function(data){
				$.each(data,function(i,goods){
					$("#orderDetailDiv>table>tbody").append(makeGoodsTr2(goods));
					//判断该商品是否是布艺帘
					
				});
			},
			"json"
	);
}


//审核成功
function updateOrderStatusByIdAndStatus(){
	var orderId=$("#orderDetailDiv>table>tbody>input").val();
	$.post(
			"../orders/shenHeDingDan.do",
			{orderId:orderId,
			status:2
			},
			function(data){
				if(data==1){
					success("审核通过成功!");
					setTimeout(myfresh,2000);
				}
			},
			"json"
	
	);
}
//制作商品的表格(加勾选)
function makeGoodsTr2(goods){
	var addpeijian="";
	if(goods.addpeijian==1){
		addpeijian="加装"
	}else {
		addpeijian="不加装"
	}
	var tr=$('<tr class="gradeA even"></tr>');
	tr.append("<td><input type='radio' name='select'></td>" +
			  "<td>" +goods.goodsId+"</td>" +
			  "<td><img src='http://oeilufnx2.bkt.clouddn.com/"+goods.sampleImg+"?imageView2/1/w/100/h/100'></td>" +
			  "<td>" +goods.price+"</td>" +
			  "<td>" +goods.chargeUnit+"</td>" +
			  "<td>"+goods.accessoriesTypeContent+"</td>" +
			  "<td>" +goods.number+"</td>" +
			  "<td>" +goods.saleNumber+"</td>" +
			  "<td>" +goods.acc+"</td>" +
			  "<td>" +addpeijian+"</td>" +
			  "<td>" +goods.sumPrice+"</td>"
			 );		

	return tr;
}
//制作商品的表格
function makeGoodsTr(goods){
	var addpeijian="";
	if(goods.addpeijian==1){
		addpeijian="加装"
	}else {
		addpeijian="不加装"
	}
	var tr=$('<tr class="gradeA even"></tr>');
	tr.append("<td>" +goods.goodsId+"</td>" +
			  "<td><img src='http://oeilufnx2.bkt.clouddn.com/"+goods.sampleImg+"?imageView2/1/w/100/h/100'></td>" +
			  "<td>" +goods.price+"</td>" +
			  "<td>" +goods.chargeUnit+"</td>" +
			  "<td>"+goods.accessoriesTypeContent+"</td>" +
			  "<td>" +goods.number+"</td>" +
			  "<td>" +goods.saleNumber+"</td>" +
			  "<td>" +goods.acc+"</td>" +
			  "<td>" +addpeijian+"</td>" +
			  "<td>" +goods.sumPrice+"</td>"
			 );		

	return tr;
}



//分页
function makeFenYe(method,page,status){
	var rootDiv=$('<div class="row-fluid">');
	var secondDiv=$('<div class="span6">');
	var threeDiv=$('<div class="dataTables_paginate paging_bootstrap pagination">')
	var ul=$('<ul>');
	var lastLi=$('<li class="prev">');
	var nextLi=$('<li class="next">');
	var aLast=$('<a class="page-link">← 上一页</a>');
	var aNext=$('<a class="page-link">下一页 → </a>');	
	if(page.currentPage>1){
		aLast.attr({onclick:method+"("+(page.currentPage-1)+","+status+")"});
	}
	if(page.currentPage<page.totalPage){
		aNext.attr({onclick:method+"("+(page.currentPage+1)+","+status+")"});
	}
	lastLi.append(aLast);
	nextLi.append(aNext);
	ul.append(lastLi);
	if(4>page.totalPage>0){
		for(var i=1;i<page.totalPage+1;i++){
			if(i==page.currentPage){
				ul.append('<li class="page-item active"><a onclick="'+method+'('+i+","+status+')">'+i+'</a></li>');
			}else{
				ul.append('<li class="page-item"><a onclick="'+method+'('+i+","+status+')">'+i+'</a></li>');
			}
			
		}   
	}
	if(page.totalPage>3){
		if(page.currentPage==1){
			ul.append('<li class="page-item active"><a onclick="'+method+'('+page.currentPage+","+status+')">'+page.currentPage+'</a></li>');
			ul.append('<li class="page-item"><a onclick="'+method+'('+(page.currentPage+1)+","+status+')">'+(page.currentPage+1)+'</a></li>');
			ul.append('<li class="page-item"><a onclick="'+method+'('+(page.currentPage+2)+","+status+')">'+(page.currentPage+2)+'</a></li>');

		}else if(page.currentPage==2){
			ul.append('<li class="page-item"><a onclick="'+method+'('+(page.currentPage-1)+","+status+')">'+(page.currentPage-1)+'</a></li>');
			ul.append('<li class="page-item active"><a onclick="'+method+'('+page.currentPage+","+status+')">'+page.currentPage+'</a></li>');
			ul.append('<li class="page-item"><a onclick="'+method+'('+(page.currentPage+1)+","+status+')">'+(page.currentPage+1)+'</a></li>');			
		}else{
			if(page.currentPage==page.totalPage){
				ul.append('<li class="page-item"><a class="page-link">...</a></li>');
				ul.append('<li class="page-item"><a onclick="'+method+'('+(page.currentPage-1)+","+status+')">'+(page.currentPage-1)+'</a></li>');
				ul.append('<li class="page-item active"><a onclick="'+method+'('+page.currentPage+","+status+')">'+page.currentPage+'</a></li>');				
				ul.append('<li class="page-item"><a class="page-link">...</a></li>');
			}else{
				ul.append('<li class="page-item"><a class="page-link">...</a></li>');
				ul.append('<li class="page-item"><a onclick="'+method+'('+(page.currentPage-1)+","+status+')">'+(page.currentPage-1)+'</a></li>');
				ul.append('<li class="page-item active"><a onclick="'+method+'('+page.currentPage+","+status+')">'+page.currentPage+'</a></li>');
				ul.append('<li class="page-item"><a onclick="'+method+'('+(page.currentPage+1)+","+status+')">'+(page.currentPage+1)+'</a></li>');
				ul.append('<li class="page-item"><a class="page-link">...</a></li>');	
			}
		}

	}
	ul.append(nextLi);
	ul.append('<li class="page-item"><a>总共'+page.totalPage+'页</a></li>');
	threeDiv.append(ul);
	secondDiv.append(threeDiv);
	rootDiv.append(secondDiv);
	return rootDiv;
}

//操作成功之后提示
function success(text){
	  $("#alerts").html("<div  style='text-align: center; display:no;' class='alert alert-success alert-dismissable'><strong>"+text+"</strong></div>");
	  $("#menus").click();
 
}

//操作失败之后提示

function fail(text){
	  $("#alerts").html("<div  style='text-align: center; display:no;' class='alert alert-success alert-dismissable'><strong>"+text+"</strong></div>");
	  $("#menus").click();

}
function myfresh(){
	window.location.href=window.location.href;
}

